<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 自动事件处理接口
 */
class Ajax extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 自动取消需求订单
     */
    public function AjaxAutoCancel(){
        $list = db('demand_order')->where('pay_status',2)->limit(100)->select();
        foreach ($list as $key => $value){
            if($value['createtime'] + 3600 < time()){
                $demand_info = db('demand')->where('id',$value['demand_id'])->where('status',',frozen')->find();
                if($demand_info){
                    $result = db('demand_order')->where('id',$value['id'])->update(['status' => 'cancel','canceltime'=>time()]);
                    if($result){
                        db('demand')->where('id',$value['demand_id'])->update(['status' => 'normal']);
                    }
                }
            }
        }
    }

    /**
     * 自动检查发布的需求是否过期
     */
    public function AjaxAutoDemand(){
        $list = db('demand')->where('status','normal')->where('is_extension|is_top',1)->limit(100)->select();
        foreach ($list as $key => $value){
            if($value['is_extension'] == 1){
                if($value['extension_end_time'] < time()){
                    $data['is_extension'] = 0;
                    $data['extension_end_time'] = null;
                    db('demand')->where('id',$value['id'])->update($data);
                }
            }

            if($value['is_top'] == 1){
                if($value['top_end_time'] < time()){
                    $data['is_top'] = 0;
                    $data['top_end_time'] = null;
                    db('demand')->where('id',$value['id'])->update($data);
                }
            }

        }
    }

    /**
     * 自动检查发布的求购是否过期
     */
    public function AjaxAutoWantToBuy(){
        $list = db('want_to_buy')->where('status','normal')->where('is_extension|is_top',1)->limit(100)->select();
        foreach ($list as $key => $value){
            if($value['is_extension'] == 1){
                if($value['extension_end_time'] < time()){
                    $data['is_extension'] = 0;
                    $data['extension_end_time'] = null;
                    db('want_to_buy')->where('id',$value['id'])->update($data);
                }
            }

            if($value['is_top'] == 1){
                if($value['top_end_time'] < time()){
                    $data['is_top'] = 0;
                    $data['top_end_time'] = null;
                    db('want_to_buy')->where('id',$value['id'])->update($data);
                }
            }

        }
    }

    /**
     * 自动检查发布的服务是否过期
     */
    public function AjaxAutoService(){
        $list = db('service')->where('status','normal')->where('is_extension|is_top',1)->limit(100)->select();
        foreach ($list as $key => $value){
            if($value['is_extension'] == 1){
                if($value['extension_end_time'] < time()){
                    $data['is_extension'] = 0;
                    $data['extension_end_time'] = null;
                    db('service')->where('id',$value['id'])->update($data);
                }
            }

            if($value['is_top'] == 1){
                if($value['top_end_time'] < time()){
                    $data['is_top'] = 0;
                    $data['top_end_time'] = null;
                    db('service')->where('id',$value['id'])->update($data);
                }
            }

        }
    }






}
